<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="./lib/vue-2.5.9.js"></script>
</head>

<body>
  <div id="app">
    firstname：
    <input type="text" v-model="firstname"> + lastname：
    <input type="text" v-model="lastname"> = fullname：
    <input type="text" v-model="fullname">

    <hr>
    <p>{{ fullname }}</p>
    <p>{{ fullname }}</p>
    <p>{{ fullname }}</p>
  </div>

  <script>
    // 创建 Vue 实例，得到 ViewModel
    var vm = new Vue({
      el: '#app',
      data: {
        firstname: '',
        lastname: ''
      },
      methods: {},
      computed: { // 计算属性区域
        'fullname': {
          get() { // get 表示外界要引用 fullname 的值
            console.log('调用了fullname的get方法')
            return this.firstname + '-' + this.lastname
          },
          set(val) { // 外界重新为 fullname 赋值了
            const parts = val.split('-')
            this.firstname = parts[0] || ''
            this.lastname = parts[1] || ''
          }
        }
      }
    });
  </script>
</body>

</html>